<template>
  <div class="addEdit">
    <common-head title="地址编辑"></common-head>
    <div class="main">
      <van-address-edit
        :area-list="dataJson"
        show-delete
        show-set-default
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        :address-info="nowAddress"
        @save="onSave"
        @delete="onDelete"
      />
    </div>
  </div>
</template>

<script>
import CommonHead from "components/CommonHead";
import { mapState, mapMutations } from "vuex";
export default {
  data() {
    return {};
  },
  methods: {
    onSave(ads) {
      console.log(ads, this.$route.params.id);
      // 条件判断 如果当前设置为了默认地址 将所有的其他的地址属性变成false
      if (ads.isDefault) {
        //   将所有的地址 isDefault属性变成 false
        this.updateChoseId(ads.id);
        this.removeDefault();
      }

      this.updateAddress({
        ads,
        index: parseInt(this.$route.params.id)
      });
      //   修改成功 回到地址列表
      let _this = this;
      this.$toast({
        type: "success",
        message: "修改成功",
        duration: 1000,
        onClose() {
          _this.$router.go(-1);
        }
      });
    },
    onDelete() {
      this.deleteAds(parseInt(this.$route.params.id));
      let _this = this;
      this.$toast({
        type: "success",
        message: "删除成功",
        duration: 1000,
        onClose() {
          _this.$router.go(-1);
        }
      });
    },
    ...mapMutations([
      "updateAddress",
      "removeDefault",
      "updateChoseId",
      "deleteAds"
    ])
  },
  computed: {
    ...mapState(["dataJson", "address"]),
    nowAddress() {
      return this.address.lists[this.$route.params.id];
    }
  },
  components: {
    CommonHead
  }
};
</script>

<style lang="less" scoped>
.addEdit {
  /deep/.van-nav-bar {
    background: #fff !important;
    .van-icon-arrow-left {
      color: #333 !important;
    }
    .van-nav-bar__title {
      color: #333 !important;
    }
  }
  .main {
    padding-top: 65px;
    padding-bottom: 50px;
    z-index: 0;
    .van-address-edit {
      /deep/.van-cell {
        line-height: 60px !important;
        .van-cell__title {
          font-size: 16px;
          color: #aaa;
        }
        .van-switch--on {
          background: #1fdb9b;
        }
      }
    }
    /deep/.van-button--danger {
      background: linear-gradient(
        90deg,
        #45dfab 0,
        #4addbd 70%,
        #4fdace 100%
      ) !important;
      border: 1px solid transparent !important;
    }
  }
}
</style>